package com.pajk.user.service.login.common;

import java.util.Date;
import java.util.Random;

import org.apache.commons.lang.time.DateUtils;

import com.pajk.user.util.ConfigUtil;

import net.pocrd.define.SecurityType;
import net.pocrd.entity.CallerInfo;
import net.pocrd.util.AESTokenHelper;
import net.pocrd.util.AesHelper;
import net.pocrd.util.Base64Util;

/**
 * @author Frank.zhang
 *        2014-05-08
 *        用户安全令牌
 */
public class TokenHelper {
	private static AESTokenHelper tokenHelper;
	private static AesHelper helper = new AesHelper(Base64Util.decode(ConfigUtil.getProperty("com.pajk.apiTokenAes")),null);
	public TokenHelper() {
		
	}
	public static AESTokenHelper getInstance() {
		if(tokenHelper == null) {
			tokenHelper = new AESTokenHelper(helper);
		}
		return tokenHelper;
	}
	
	public static String generateUserToken(int appId,long deviceId,long uId,long expire,byte[] key) {
		CallerInfo caller = new CallerInfo();
		caller.appid = appId;
		caller.deviceId = deviceId;
		caller.uid = uId;
		caller.expire = expire;
		caller.key = key;
		if(uId > 0){
			caller.securityLevel = SecurityType.RegisteredDevice.authorize(SecurityType.UserLogin.authorize(0));
		}else{
			caller.securityLevel = SecurityType.RegisteredDevice.authorize(0);
		}
		return getInstance().generateStringToken(caller);
	}
	
	public static long generaterUserDeviceId() {
		System.out.println(123);
		Random random = new Random();
		return random.nextLong();
	}
	
	public static long generaterExpire() {
		return DateUtils.addMinutes(new Date(), 300).getTime();
	}
}
